package main

import (
	"context"
	"fmt"
	"net/http"
	"net/url"
	"time"

	"github.com/tencentyun/cos-go-sdk-v5"
)

func main() {
	u, err := url.Parse("https://coolcar-1305922819.cos.ap-guangzhou.myqcloud.com")
	if err != nil {
		panic(err)
	}
	b := &cos.BaseURL{BucketURL: u}
	// 1.永久密钥
	SecretId := "AKIDFI5csqXyB9rJUNzKPEn57rLZWEgsoTry"
	SecretKey := "rTpwmLkxvkNlulMfhSwDEyRdYCZgaFdk"

	client := cos.NewClient(b, &http.Client{
		Transport: &cos.AuthorizationTransport{
			SecretID:  SecretId,  // 替换为用户的 SecretId，请登录访问管理控制台进行查看和管理，https://console.cloud.tencent.com/cam/capi
			SecretKey: SecretKey, // 替换为用户的 SecretKey，请登录访问管理控制台进行查看和管理，https://console.cloud.tencent.com/cam/capi
		},
	})

	// 获取预签名获取对象URL
	/**
	name := "test.jpg"
	presignedURL, err := client.Object.GetPresignedURL(context.Background(), http.MethodGet, name, SecretId, SecretKey, time.Second*10, nil)
	if err != nil {
		panic(err)
	}
	fmt.Println("presignedURL", presignedURL)
	*/

	// 生成预签名获取对象URL
	name := "acacacac.jpg"
	presignedURL, err := client.Object.GetPresignedURL(context.Background(), http.MethodPut, name, SecretId, SecretKey, time.Second*60*60, nil)
	if err != nil {
		panic(err)
	}
	if err != nil {
		panic(err)
	}
	presignedURL.String()
	fmt.Println("presignedURL", presignedURL)

	// 获取文件对象
	client.Object.Get(context.Background(), "acacacac.jpg", nil)
}
